package tabbedCanvas;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import drawPropertyCommands.DrawProperties;
import drawPropertyCommands.GraphicsProperties;


/**
 * The LayeredCanvas is the main canvas where all the turtles and their trails can be seen.
 * It is a JLayeredPane with two main levels; one for the trails and the other for the turtles.
 * There will be one LayeredCanvas per tab in the main GUI.
 * 
 * @author KevinWang
 *
 */
public class LayeredCanvas extends JLayeredPane{

    private static final long serialVersionUID = 1L;
    private JPanel background;
    private TurtleLayer turtleLayer;
    private int tabID;
    private Dimension dimension;
    private Map<Integer, ArrayList<TrailLayer>> turtleTrailMap = new HashMap<Integer, ArrayList<TrailLayer>>();
    
    public LayeredCanvas(Dimension dimension , int tabID){
        this.dimension = dimension;
        initializeCanvas(tabID);
        background = addBackground();
        turtleLayer=addTurtleLayer();
    }

    /**
     * @param tabID
     */
    protected void initializeCanvas(int tabID) {
        setOpaque(true);
        setPreferredSize(dimension);
        setVisible(true);
        this.tabID= tabID;
    }
    
    /**
     * initialize background and add it to the layers
     * @return background- JPanel representing the background
     */
    private JPanel addBackground() {
        JPanel background = new JPanel();
        background.setPreferredSize(dimension);
        background.setBackground(Color.white);
        background.setBounds(0,0, (int)dimension.getWidth(), (int)dimension.getHeight());
        background.setOpaque(true);
        background.setVisible(true);
        this.add(background, JLayeredPane.DEFAULT_LAYER);
        return background;
    }
    
    /**
     * Initialize the turtleLayer and add it to the layers
     * @return turtleLayer - the JPanel representing the turtleLayer
     */
    private TurtleLayer addTurtleLayer(){
        TurtleLayer turtleLayer = new TurtleLayer(dimension);
        this.add(turtleLayer, JLayeredPane.MODAL_LAYER);
        return turtleLayer;
    }
    
    /**
     * change background color
     * @param color- the color which the background will be set to
     */
    public void setBackground(Color color){
        background.setBackground(color);
    }
    
    /**
     * 
     * move turtle give ID from x1,y1 to x2,y2 and change orientation to degrees.
     * Trail is drawn if boolean trail is true.
     * @param x1 - x coordinate of original location
     * @param y1 - y coordinate of original location
     * @param x2 - x coordinate of final location
     * @param y2 - y coordinate of final location
     * @param degrees - orientation of the turtle
     * @param trail - whether there should be a trail or not
     * @param ID - ID of the turtle to be moved
     * @param drawProperties - properties to draw the turtle trail with
     */
    public void move( int x1, int y1, int x2, int y2, int degrees, boolean trail, int ID, DrawProperties drawProperties){
        turtleLayer.move(ID, x2, y2, degrees);
        if(trail){
            drawTrail(x1, y1, x2, y2, ID, drawProperties);
        }
    }
    
    /**
     * draw trail on a TrailLayer from point x1,y1 to x2,y2 with Stroke s.
     * Add the layer to the turtleLayerMap
     * @param x1 - x coordinate of original location
     * @param y1 - y coordinate of original location
     * @param x2 - x coordinate of final location
     * @param y2 - y coordinate of final location
     * @param ID - ID of the turtle to be moved
     * @param drawProperties - properties to draw the turtle trail with
     */
    public void drawTrail(int x1, int y1, int x2, int y2, int ID, DrawProperties drawProperties){
        TrailLayer tl = new TrailLayer(dimension, drawProperties);
        tl.draw(x1, y1, x2, y2);
        this.add(tl, PALETTE_LAYER);
        addTrailToMap(ID, tl);
    }

    /**
     * add TrailLayer to the turtleTrailMap given ID
     * @param ID - ID of the turtle that is producing the Trail.
     * @param trailLayer - the TrailLayer produced by the turtle moving
     */
    private void addTrailToMap(int ID, TrailLayer trailLayer) {
        if(!turtleTrailMap.containsKey(ID)){
            turtleTrailMap.put(ID, new ArrayList<TrailLayer>(Arrays.asList(trailLayer)));
        }
        else{
            turtleTrailMap.get(ID).add(trailLayer);
        }
    }
    
    /**
     * add a new turtle to the turtle layer
     * @param ID - turtle ID
     */
    public void addTurtle(int ID){
        turtleLayer.createTurtle(ID);
    }
    
    /**
     * Set the turtle active if display is true or else set the turtle to inactive
     * @param ID - ID of the turtle to be manipulated
     * @param display - boolean indicating its activeness
     */
    public void displayTurtle(int ID, boolean display){
       if(display){
           turtleLayer.setActive(ID);
       }
       else{
           turtleLayer.hideTurtle(ID);
       }
    }
    
}
